'use strict';

/**
 * @ngdoc directive
 * @name App.directive:dateRangePicker
 * @description
 * # dateRangePicker
 */
angular.module('App')
  .directive('dateRangePicker', function () {
    return {
      restrict: 'A',
      require: 'ngModel',
      link: function(scope, element, attrs, ngModel) {
        var options = $.extend(true, {
          "width": 190,
          "opens": "left",
          "timePicker": false,
          "timePicker24Hour": false,
          "alwaysShowCalendars": true,
          "linkedCalendars": false,
          "autoApply": true,
          "autoUpdateInput": false,
          "singleDatePicker": false,
          "locale": {
            "format": "YYYY/MM/DD",
            "separator": ' - ',
            "applyLabel": "确定",
            "cancelLabel": "取消"
          }
        }, scope.$eval(attrs.dateRangePicker) || {});

        element
          .css('width', options.width)
          .daterangepicker(options, function(start, end) {
            var formatDate = options.singleDatePicker === true ? start.format(options.locale.format) : (start.format(options.locale.format) + options.locale.separator + end.format(options.locale.format));
            element.val(formatDate);
            ngModel.$setViewValue(formatDate);
          });
      }
    };
  });
